﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using Traceability.Domain;
using Traceability.ErrorCode;
using Traceability.Infrastructure;

namespace Traceability.App.Applications.Slaughter
{
    public class SlaughterQueryCommandHandler : IRequestHandler<SlaughterQueryCommand, APIResult<T_Slaughter>>
    {
        private readonly IBaseRepository<T_Slaughter> slaughterRepository;

        public SlaughterQueryCommandHandler(IBaseRepository<T_Slaughter> slaughterRepository)
        {
            this.slaughterRepository = slaughterRepository;
        }

        public Task<APIResult<T_Slaughter>> Handle(SlaughterQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<T_Slaughter> result = new APIResult<T_Slaughter>();
            result.Code = APIEnum.ok;
            result.Message = "获取成功";
            result.Data = slaughterRepository.GetAll().AsNoTracking().FirstOrDefault(x => x.Batch == request.Batch);
            return Task.FromResult(result);
        }   
    }
}
